﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBAccessLib;
using System.Data;

namespace LineWatch_Web
{
    public partial class ProductManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();//显示当天所有流水线
                if (ddlLine.Items.Count > 0)
                {
                    int i = 0;
                    //GetInteval();
                    if (Request["line"] != null)
                    {
                        string line = Request["line"].ToString();
                        i = ddlLine.Items.IndexOf(ddlLine.Items.FindByValue(line));
                        ddlLine.SelectedIndex = i;
                        hidLine.Value = line;
                    }
                    BindData(ddlLine.Items[i].Value.ToString());//显示数据
                    GetInteval();
                }
            }
        }

        private void GetInteval()
        {
            DataRow row = Product.GetLineWatchRecord(ddlLine.SelectedValue, "ScreenT");
            if (row != null)
            {
                hidTime.Value = row["Content1"].ToString().Trim();
                if (hidTime.Value.Equals("")) hidTime.Value = "5";
            }
            else hidTime.Value = "5";
        }

        private void BindData(string lineCode)
        {
            DataRow row = Product.GetProductInfo(lineCode);
            if (row == null)
            {
                labPlan.Text = "0";
                labNow.Text = "0";
                labProduct.Text = "0";
                labStoptime.Text = "0";
            }
            else
            {
                labPlan.Text = row["PlanNum"].ToString();
                labNow.Text = row["NowPlan"].ToString();
                labProduct.Text = row["ProductNum"].ToString();
                labStoptime.Text = row["StopTime"].ToString();
            }
            hidLine.Value = lineCode;
        }

        private void BindList()
        {
            DataTable dt = Product.GetAllLines();
            ddlLine.Items.Clear();
            foreach (DataRow row in dt.Rows)
            {
                ListItem li = new ListItem(row["Line_Name"].ToString().Trim(), row["Line_Code"].ToString().Trim());
                ddlLine.Items.Add(li);
            }
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            if (ddlLine.Items.Count > 0)
            {
                hidLine.Value = ddlLine.SelectedValue;
                BindData(ddlLine.SelectedItem.Value.ToString());
            }
        }
    }
}